{
 "metadata": {
  "name": "pgmPy"
 }, 
 "nbformat": 2, 
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "", 
      "", 
      "a=Factor([ 3, 1, 2], [ 2, 2, 2], [ 0, 0, 0, 0, 0, 0, 0, 0 ] )", 
      "b=Factor( [1], [2], [ .11, .89] )", 
      "c=Factor( [3,2], [2,2], [0.39, 0.61, 0.06, 0.94] )"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "A=IndexToAssignment( np.arange(0, np.prod( a.getCard() )), a.getCard() )", 
      "B=IndexToAssignment( np.arange(0, np.prod( b.getCard() )), b.getCard() )", 
      "C=IndexToAssignment( np.arange(0, np.prod( c.getCard() )), c.getCard() )", 
      "", 
      "print A", 
      "#print B", 
      "#print C", 
      "", 
      "Ia=AssignmentToIndex(A, a.getCard() )", 
      "Ib=(AssignmentToIndex(B, b.getCard() ))", 
      "Ic=(AssignmentToIndex(C, c.getCard() ))", 
      "print Ia", 
      "#print Ia", 
      "#print Ic"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]]", 
        "[[ 1.]", 
        " [ 2.]", 
        " [ 3.]", 
        " [ 4.]", 
        " [ 5.]", 
        " [ 6.]", 
        " [ 7.]", 
        " [ 8.]]"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#print Ia", 
      "values=[-0.1241, 1.4897, 1.4090, 1.4172,0.6715,-1.2075, 0.7172, 1.6302]", 
      "values_b=[.89,.11]", 
      "values_c=[.94,.06,.61,.39]", 
      "print a", 
      "print b", 
      "print c"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "0 0 0 0 0 0 0 0", 
        "1", 
        "2", 
        "0.11 0.89", 
        "3 2", 
        "2 2", 
        "0.39 0.61 0.06 0.94"
       ]
      }
     ], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "SetValueOfAssignment(a,A,values)", 
      "SetValueOfAssignment(b,B, values_b)", 
      "SetValueOfAssignment(c,C, values_c)"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "np.shape( GetValueOfAssignment(b,B) )", 
      "GetValueOfAssignment(a,A)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 5, 
       "text": [
        "array([[-0.1241,  1.4897,  1.409 ,  1.4172,  0.6715, -1.2075,  0.7172,", 
        "         1.6302]])"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a", 
      "print b", 
      "print c"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "-0.1241 1.4897 1.409 1.4172 0.6715 -1.2075 0.7172 1.6302", 
        "1", 
        "2", 
        "0.89 0.11", 
        "3 2", 
        "2 2", 
        "0.94 0.06 0.61 0.39"
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "d=FactorProduct(a,b)"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a, b, d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "-0.1241 1.4897 1.409 1.4172 0.6715 -1.2075 0.7172 1.6302 1", 
        "2", 
        "0.89 0.11 1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 9
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "aMap=isMember(a.getVar(), d.getVar() )", 
      "bMap=isMember(b.getVar(), d.getVar() )"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 10
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a.getVar()", 
      "print b.getVar()", 
      "print d.getVar()"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[3 1 2]", 
        "[1]", 
        "[1 2 3]"
       ]
      }
     ], 
     "prompt_number": 11
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print aMap", 
      "print bMap"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[2, 0, 1]", 
        "[0]"
       ]
      }
     ], 
     "prompt_number": 12
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "d.setCard( np.zeros( len(d.getVar())).tolist() )"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 13
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "0.0 0.0 0.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 14
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "d.getCard()[aMap]=a.getCard()", 
      "d.getCard()[bMap]=b.getCard()"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 15
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 16
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a", 
      "print b", 
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "-0.1241 1.4897 1.409 1.4172 0.6715 -1.2075 0.7172 1.6302", 
        "1", 
        "2", 
        "0.89 0.11", 
        "1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 17
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "d=FactorProduct(a,b)", 
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 18
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "d.setVal( np.zeros(np.prod(d.getCard())).tolist() )"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 19
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "2.0 2.0 2.0", 
        "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
       ]
      }
     ], 
     "prompt_number": 20
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "np.prod(d.getCard())"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 21, 
       "text": [
        "8.0"
       ]
      }
     ], 
     "prompt_number": 21
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "assignments=IndexToAssignment( np.arange(np.prod(d.getCard())), d.getCard() )"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 22
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print assignments"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]]"
       ]
      }
     ], 
     "prompt_number": 23
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print Ia"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.]", 
        " [ 2.]", 
        " [ 3.]", 
        " [ 4.]", 
        " [ 5.]", 
        " [ 6.]", 
        " [ 7.]", 
        " [ 8.]]"
       ]
      }
     ], 
     "prompt_number": 24
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print aMap", 
      "print bMap", 
      "print a.getVar()", 
      "print b.getVar()"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[2, 0, 1]", 
        "[0]", 
        "[3 1 2]", 
        "[1]"
       ]
      }
     ], 
     "prompt_number": 25
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#print assignments", 
      "print np.shape ( assignments )", 
      "indxA=AssignmentToIndex(  assignments[:,aMap], a.getCard())-1", 
      "indxB=AssignmentToIndex(  assignments[:,bMap], b.getCard())-1"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "(8, 3)"
       ]
      }
     ], 
     "prompt_number": 26
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print assignments", 
      "print \"===\"", 
      "print assignments[:,aMap]"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]]", 
        "===", 
        "[[ 1.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  1.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 2.  1.  2.]", 
        " [ 2.  2.  2.]]"
       ]
      }
     ], 
     "prompt_number": 27
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a", 
      "print b"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "-0.1241 1.4897 1.409 1.4172 0.6715 -1.2075 0.7172 1.6302", 
        "1", 
        "2", 
        "0.89 0.11"
       ]
      }
     ], 
     "prompt_number": 28
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print indxA", 
      "print indxB"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 0.]", 
        " [ 2.]", 
        " [ 4.]", 
        " [ 6.]", 
        " [ 1.]", 
        " [ 3.]", 
        " [ 5.]", 
        " [ 7.]]", 
        "[[ 0.]", 
        " [ 1.]", 
        " [ 0.]", 
        " [ 1.]", 
        " [ 0.]", 
        " [ 1.]", 
        " [ 0.]", 
        " [ 1.]]"
       ]
      }
     ], 
     "prompt_number": 29
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "d_val=a.getVal()[indxA.flatten().tolist()] * b.getVal()[indxB.flatten().tolist()]"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 30
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print d_val"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[-0.110449  0.15499   0.597635  0.078892  1.325833  0.155892 -1.074675", 
        "  0.179322]"
       ]
      }
     ], 
     "prompt_number": 31
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "d_val"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 32, 
       "text": [
        "array([-0.110449,  0.15499 ,  0.597635,  0.078892,  1.325833,  0.155892,", 
        "       -1.074675,  0.179322])"
       ]
      }
     ], 
     "prompt_number": 32
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a", 
      "print \"==\"", 
      "print b", 
      "print \"==\"", 
      "print d", 
      "print \"==\"", 
      "d=FactorProduct(a,b)", 
      "print d"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "3 1 2", 
        "2 2 2", 
        "-0.1241 1.4897 1.409 1.4172 0.6715 -1.2075 0.7172 1.6302", 
        "==", 
        "1", 
        "2", 
        "0.89 0.11", 
        "==", 
        "1 2 3", 
        "2.0 2.0 2.0", 
        "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0", 
        "==", 
        "1 2 3", 
        "2.0 2.0 2.0", 
        "-0.110449 0.15499 0.597635 0.078892 1.325833 0.155892 -1.074675 0.179322"
       ]
      }
     ], 
     "prompt_number": 33
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#example from figure 4.3 in Koller and Friedman textbook", 
      "a1=Factor([  1, 2], [ 3, 2], [ 0, 0, 0, 0, 0, 0, 0, 0 ] )", 
      "b1=Factor( [2,3], [2,2], [ 0,0] )", 
      "", 
      "assignmentA=IndexToAssignment( np.arange(0, np.prod( a1.getCard() )), a1.getCard() )", 
      "assignmentB=IndexToAssignment( np.arange(0, np.prod( b1.getCard() )), b1.getCard() )", 
      "#print assignmentA", 
      "#print assignmentB", 
      "", 
      "valuesA=[.5,.1,.3,.8,0,.9]", 
      "valuesB=[.5,.1,.7,.2]", 
      "", 
      "SetValueOfAssignment(a1,assignmentA,valuesA)", 
      "SetValueOfAssignment(b1,assignmentB,valuesB)", 
      "", 
      "", 
      "c1=FactorProduct(a1,b1)", 
      "", 
      "assignmentC=IndexToAssignment(np.arange(0,np.prod( c1.getCard() )), c1.getCard() )", 
      "Ic1=AssignmentToIndex(assignmentC, c1.getCard() )", 
      "print assignmentC", 
      "print GetValueOfAssignment(c1,assignmentC)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 3.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 3.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 3.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]", 
        " [ 3.  2.  2.]]", 
        "[[ 0.25  0.05  0.15  0.08  0.    0.09  0.35  0.07  0.21  0.16  0.    0.18]]"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#figure 9.7 factor marginalization in Koller & Friedman pg. 297", 
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "a1=Factor( [ 1,2,3], [ 3, 2, 2], [ 0,0,0,0,0,0,0,0,0,0,0,0] )", 
      "assignmentA=IndexToAssignment( np.arange(0, np.prod( a1.getCard() )), a1.getCard() )", 
      "valuesA=[.25,.05,.15,.08,0,0.09,0.35,.07,.21,.16,0,.18]", 
      "SetValueOfAssignment(a1,assignmentA,valuesA)", 
      "print assignmentA", 
      "print GetValueOfAssignment(a1,assignmentA)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[[ 1.  1.  1.]", 
        " [ 2.  1.  1.]", 
        " [ 3.  1.  1.]", 
        " [ 1.  2.  1.]", 
        " [ 2.  2.  1.]", 
        " [ 3.  2.  1.]", 
        " [ 1.  1.  2.]", 
        " [ 2.  1.  2.]", 
        " [ 3.  1.  2.]", 
        " [ 1.  2.  2.]", 
        " [ 2.  2.  2.]", 
        " [ 3.  2.  2.]]", 
        "[[ 0.25  0.05  0.15  0.08  0.    0.09  0.35  0.07  0.21  0.16  0.    0.18]]"
       ]
      }
     ], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print a1"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "3 2 2", 
        "0.25 0.05 0.15 0.08 0.0 0.09 0.35 0.07 0.21 0.16 0.0 0.18"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "V=[2]", 
      "setA=set( a1.getVar() )", 
      "setV=set(V)", 
      "Bvar=np.array( list( setA.difference(setV)))", 
      "#print np.shape(Bvar)", 
      "#print np.shape(a1.getVar())", 
      "#print Bvar", 
      "#print a1.getVar()", 
      "mapB=isMember(Bvar, a1.getVar())", 
      "#A=a1.getVar()", 
      "#for i in range( len(Bvar)):", 
      "    #print i", 
      "#    print np.where(A==Bvar[i])[0]", 
      "#print mapB", 
      "marginFactor=FactorMarginalization(a1,V)", 
      "len(a1.getVal())", 
      "#print np.arange(np.prod(a1.getCard()) )", 
      "#print a1.getCard()", 
      "assignments=IndexToAssignment ( np.arange(np.prod(a1.getCard()) ), a1.getCard() )", 
      "#print assignments", 
      "#indxB=AssignmentToIndex( assignments[:,mapB], marginFactor.getCard())-1", 
      "", 
      "marginFactorAssignments= IndexToAssignment( np.arange(np.prod(marginFactor.getCard() ) ), marginFactor.getCard() )", 
      "", 
      "print marginFactor ", 
      "print marginFactorAssignments", 
      "print GetValueOfAssignment(marginFactor, marginFactorAssignments)", 
      "#print indxB", 
      "#np.shape(indxB)", 
      "#print assignments", 
      "#indxB=AssignmentToIndex(  assignments[:,mapB], marginFactor.getCard())-1"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[0, 2] [1 3]", 
        "[[ 0.]", 
        " [ 1.]", 
        " [ 2.]", 
        " [ 0.]", 
        " [ 1.]", 
        " [ 2.]", 
        " [ 3.]", 
        " [ 4.]", 
        " [ 5.]", 
        " [ 3.]", 
        " [ 4.]", 
        " [ 5.]]", 
        "[ 0.25  0.05  0.15  0.08  0.    0.09  0.35  0.07  0.21  0.16  0.    0.18]", 
        "1 3", 
        "3 2", 
        "0.33 0.05 0.24 0.51 0.07 0.39", 
        "[[ 1.  1.]", 
        " [ 2.  1.]", 
        " [ 3.  1.]", 
        " [ 1.  2.]", 
        " [ 2.  2.]", 
        " [ 3.  2.]]", 
        "[[ 0.33  0.05  0.24  0.51  0.07  0.39]]"
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "from itertools import product", 
      "def accum(accmap, a, func=None, size=None, fill_value=0, dtype=None):", 
      "", 
      "    if accmap.shape[:a.ndim] != a.shape:", 
      "        raise ValueError(\"The initial dimensions of accmap must be the same as a.shape\")", 
      "    if func is None:", 
      "        func = np.sum", 
      "    if dtype is None:", 
      "        dtype = a.dtype", 
      "    if accmap.shape == a.shape:", 
      "        accmap = np.expand_dims(accmap, -1)", 
      "    adims = tuple(range(a.ndim))", 
      "    if size is None:", 
      "        size = 1 + np.squeeze(np.apply_over_axes(np.max, accmap, axes=adims))", 
      "        size = np.atleast_1d(size)", 
      "    size=np.array(size, dtype=int)", 
      "    # Create an array of python lists of values.", 
      "    vals = np.empty(size, dtype='O')", 
      "    for s in product(*[range(k) for k in size]):", 
      "        vals[s] = []", 
      "    for s in product(*[range(k) for k in a.shape]):", 
      "        indx = tuple(accmap[s])", 
      "        val = a[s]", 
      "        vals[indx].append(val)", 
      "   ", 
      "   # Create the output array.", 
      "    out = np.empty(size, dtype=dtype)", 
      "    for s in product(*[range(k) for k in size]):", 
      "        if vals[s] == []:", 
      "            out[s] = fill_value", 
      "        else:", 
      "            out[s] = func(vals[s])", 
      "  ", 
      "    return out"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "foo=np.array([[1,2,3],[4,-1,6],[-1,8,9]])", 
      "accumap= np.array([[0,1,2],[2,0,1],[1,2,0]])", 
      "print accumap.shape[:2]", 
      "print accumap[:2]", 
      "", 
      "accum(accumap,foo)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "(3, 3)", 
        "[[0 1 2]", 
        " [2 0 1]]"
       ]
      }, 
      {
       "output_type": "pyout", 
       "prompt_number": 15, 
       "text": [
        "array([ 9,  7, 15])"
       ]
      }
     ], 
     "prompt_number": 15
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print np.shape ( a1.getVal() )", 
      "print np.shape(indxB)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "(12,)", 
        "(12, 1)"
       ]
      }
     ], 
     "prompt_number": 41
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "accum(indxB, a1.getVal() )"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 42, 
       "text": [
        "array([ 0.33,  0.05,  0.24,  0.51,  0.07,  0.39])"
       ]
      }
     ], 
     "prompt_number": 42
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "bar=np.array([6.])", 
      "baz=np.array(bar, dtype=int)", 
      "range(bar)", 
      "range(baz)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 43, 
       "text": [
        "[0, 1, 2, 3, 4, 5]"
       ]
      }
     ], 
     "prompt_number": 43
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#factor reduction - Observe evidence update values of a factor based on evidence", 
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "", 
      "", 
      "# make a list of factors", 
      "", 
      "f1=Factor([1], [2], [.11, .89] )", 
      "f2=Factor([2, 1], [2, 2],[.59,.41,.22,.78])", 
      "f3=Factor([3,2], [2,2], [.39,.61,.06,.94])", 
      "", 
      "#list of factors", 
      "INPUTS=[ f1, f2, f3]", 
      "", 
      "#evidence is an Nx2 np.array", 
      "# first column is variable and second column is evidence(value of variable)", 
      "EVIDENCE= np.array([[2, 1], [3, 2]])", 
      "print np.shape(EVIDENCE)", 
      "for f in INPUTS:", 
      "    print f", 
      "    print \"===\"", 
      "", 
      "    ", 
      "print \"==\"", 
      "print EVIDENCE"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "(2, 2)", 
        "1", 
        "2", 
        "0.11 0.89", 
        "===", 
        "2 1", 
        "2 2", 
        "0.59 0.41 0.22 0.78", 
        "===", 
        "3 2", 
        "2 2", 
        "0.39 0.61 0.06 0.94", 
        "===", 
        "==", 
        "[[2 1]", 
        " [3 2]]"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "(nrows, ncols)=np.shape(EVIDENCE)", 
      "total_factors=len(INPUTS)", 
      "#iterate through evidence", 
      "for i in range(nrows):", 
      "    variable=EVIDENCE[i,0]", 
      "    value=EVIDENCE[i,1]", 
      "    #print 'var: ', variable, 'value: ', value", 
      "    if int(value) == 0:", 
      "        print \"Evidence is not set for variable: ', variable, ' in evidence matrix.\\n\"", 
      "        continue", 
      "    for factor in INPUTS:", 
      "        #the following returns a list", 
      "        indx=np.where( factor.getVar() == variable )[0].tolist()", 
      "        if indx: #if the indx is not empty, it contains the index value", 
      "                 # of the evidence variable in factor.val array", 
      "            indx=indx[0] #assign indx to be the actually value", 
      "            if value > factor.getCard()[indx] or value < 0:", 
      "                sys.stderr.write(\"invalid evidene for variable X_'\" + str(variable) + \" = \" + str(value) + \"\\n\")", 
      "                sys.exit(1)", 
      "            assignments=IndexToAssignment( np.arange(np.prod( factor.getCard() )), factor.getCard() )", 
      "            #print assignments[:,indx]", 
      "            mask=np.where( assignments[:,indx] != value )[0].tolist()", 
      "            #print mask", 
      "            newvals=factor.getVal()", 
      "            newvals[mask]=0", 
      "            factor.setVal( newvals.tolist() )", 
      "            ", 
      "            zeroIndices=np.where ( factor.getVal() == 0)[0].tolist()", 
      "            if len(zeroIndices) == len (factor.getVal() ):", 
      "                sys.stderr.write(\"All variable values are zero, which is not possible.\\n\")", 
      "                ", 
      "        ", 
      "            #print factor", 
      "    ", 
      "            "
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 42
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "for f in INPUTS:", 
      "    print f", 
      "    print \"===\""
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1", 
        "2", 
        "0.11 0.89", 
        "===", 
        "2 1", 
        "2 2", 
        "0.59 0.0 0.22 0.0", 
        "===", 
        "3 2", 
        "2 2", 
        "0.0 0.61 0.0 0.0", 
        "==="
       ]
      }
     ], 
     "prompt_number": 43
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "ObserveEvidence(INPUTS, EVIDENCE)"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "for f in INPUTS:", 
      "    print f", 
      "    print \"===\""
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1", 
        "2", 
        "0.11 0.89", 
        "===", 
        "2 1", 
        "2 2", 
        "0.59 0.0 0.22 0.0", 
        "===", 
        "3 2", 
        "2 2", 
        "0.0 0.61 0.0 0.0", 
        "==="
       ]
      }
     ], 
     "prompt_number": 6
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "#compute joint distribution", 
      "# with the help of FactorProduct", 
      "#factor reduction - Observe evidence update values of a factor based on evidence", 
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "", 
      "", 
      "# make a list of factors", 
      "", 
      "f1=Factor([1], [2], [.11, .89] )", 
      "f2=Factor([2, 1], [2, 2],[.59,.41,.22,.78])", 
      "f3=Factor([3,2], [2,2], [.39,.61,.06,.94])", 
      "", 
      "#list of factors", 
      "INPUTS=[ f1, f2, f3]"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "joint=ComputeJointDistribution(INPUTS)", 
      "#totalFactors = len(INPUTS)", 
      "", 
      "#if totalFactors == 2:", 
      "#    joint=FactorProduct( INPUTS[0], INPUTS[1] )", 
      "#joint=reduce(lambda x, y: FactorProduct(x,y), INPUTS)"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "for f in INPUTS:", 
      "    print f", 
      "print \"===\"", 
      "print joint"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1", 
        "2", 
        "0.11 0.89", 
        "2 1", 
        "2 2", 
        "0.59 0.41 0.22 0.78", 
        "3 2", 
        "2 2", 
        "0.39 0.61 0.06 0.94", 
        "===", 
        "1 2 3", 
        "2.0 2.0 2.0", 
        "0.025311 0.076362 0.002706 0.041652 0.039589 0.119438 0.042394 0.652548"
       ]
      }
     ], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "#Compute Marginal distribution, givne evidence", 
      "#compute joint distribution", 
      "# with the help of FactorProduct", 
      "#factor reduction - Observe evidence update values of a factor based on evidence", 
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "", 
      "", 
      "# make a list of factors", 
      "", 
      "f1=Factor([1], [2], [.11, .89] )", 
      "f2=Factor([2, 1], [2, 2],[.59,.41,.22,.78])", 
      "f3=Factor([3,2], [2,2], [.39,.61,.06,.94])", 
      "", 
      "#list of factors", 
      "INPUTS=[ f1, f2, f3]", 
      "M=ComputeMarginal( [2,3], INPUTS, [1,2])", 
      "print M"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "2 3", 
        "2.0 2.0", 
        "0.0858 0.0468 0.1342 0.7332"
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "a=np.array( [ [2,3], [3,1] ])", 
      "np.shape(a)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 4, 
       "text": [
        "(2, 2)"
       ]
      }
     ], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "a=np.array ( [2,3] )", 
      "np.shape(a)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 5, 
       "text": [
        "(2,)"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "a=np.reshape( a, (-1,2))"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "np.shape(a)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 8, 
       "text": [
        "(1, 2)"
       ]
      }
     ], 
     "prompt_number": 8
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print ComputeJointDistribution(INPUTS)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "1 2 3", 
        "2.0 2.0 2.0", 
        "0.025311 0.076362 0.002706 0.041652 0.039589 0.119438 0.042394 0.652548"
       ]
      }
     ], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "np.sum(INPUTS[0].getVal() )"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 2, 
       "text": [
        "1.0"
       ]
      }
     ], 
     "prompt_number": 2
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "#now doing the simpliest thing that can possible work", 
      "#lets construct a simple Genetic couselling PGM based on ", 
      "# PGM assignment 2", 
      "import numpy as np", 
      "from Factor import *", 
      "from FactorOperations import *", 
      "from PGMcommon import *", 
      "from PedigreeFactors import *"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "foo=PhenotypeFactor(0,2,5,'Gregor')"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 4
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print foo"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "name: Gregor", 
        "var: 5 2", 
        "card: 2 3", 
        "val: 0.0 1.0 0.0 1.0 1.0 0.0"
       ]
      }
     ], 
     "prompt_number": 5
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "np.zeros( np.prod([2,3]))"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 7, 
       "text": [
        "array([ 0.,  0.,  0.,  0.,  0.,  0.])"
       ]
      }
     ], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "IndexToAssignment( np.arange(0,np.prod( [2,3])), [2,3])"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "pyout", 
       "prompt_number": 11, 
       "text": [
        "array([[ 1.,  1.],", 
        "       [ 2.,  1.],", 
        "       [ 1.,  2.],", 
        "       [ 2.,  2.],", 
        "       [ 1.,  3.],", 
        "       [ 2.,  3.]])"
       ]
      }
     ], 
     "prompt_number": 11
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [
      "# in variable elimination, you need to peform elimination seperatly for each variable", 
      "# you wish to make inferences on", 
      "# CTs offer a general data structure", 
      "# read the textbook on this ....", 
      "", 
      "# variable elimination ====> clique tree", 
      "# clique tree ======> variable elimination", 
      "", 
      "", 
      "#now does polymut perform VE for each individual separately?", 
      "# if so, we can use the sum/product BP algorithm"
     ], 
     "language": "python", 
     "outputs": [], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "from Factor import *", 
      "from PGMcommon import *", 
      "from FactorOperations import *", 
      "", 
      "A=Factor([2], [3], [.01,.18,.81], 'A')", 
      "B=Factor([2,8], [3,2],[.8,.6,.10,.20,.40,.90], 'B')", 
      "#print A", 
      "#print B", 
      "C=FactorProduct(A,B)"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "A.getVar():   [2]", 
        "B.getVar():  [2 8]", 
        "setA  set([2])", 
        "setB  set([8, 2])", 
        "[8, 2]", 
        "name: None", 
        "var: 8 2", 
        "card: ", 
        "val: "
       ]
      }
     ], 
     "prompt_number": 1
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "A=np.array ( [2] )", 
      "B=np.array( [2,8] )", 
      "setA=set(A)", 
      "setB=set(B)", 
      "print list( setA.union(setB) )", 
      "np.union1d(A,B).tolist()"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[8, 2]"
       ]
      }, 
      {
       "output_type": "pyout", 
       "prompt_number": 7, 
       "text": [
        "[2, 8]"
       ]
      }
     ], 
     "prompt_number": 7
    }, 
    {
     "cell_type": "code", 
     "collapsed": false, 
     "input": [
      "print C"
     ], 
     "language": "python", 
     "outputs": [
      {
       "output_type": "stream", 
       "stream": "stdout", 
       "text": [
        "[2 8]"
       ]
      }
     ], 
     "prompt_number": 3
    }, 
    {
     "cell_type": "code", 
     "collapsed": true, 
     "input": [], 
     "language": "python", 
     "outputs": []
    }
   ]
  }
 ]
}